from django.db import models


class User(models.Model):
    SEX_CHOICE = [
        (0, '未公开'),
        (1, '男'),
        (2, '女')
    ]

    user_id = models.CharField(max_length=50, primary_key=True, verbose_name='用户账号')
    user_name = models.CharField(max_length=50, null=True, blank=True, verbose_name='用户姓名')
    user_nick = models.CharField(max_length=50, null=True, blank=True, verbose_name='用户昵称')
    user_icon = models.TextField(null=True, blank=True, verbose_name='用户头像(base64)')
    user_introduce = models.CharField(max_length=500, null=True, blank=True, verbose_name='用户介绍')
    user_sex = models.SmallIntegerField(default=0, choices=SEX_CHOICE, verbose_name='用户性别')
    student_id = models.CharField(max_length=50, null=True, blank=True, verbose_name='用户学号')
    user_power = models.BooleanField(default=False, verbose_name='是否管理员')
    user_telephone = models.CharField(max_length=15, null=True, blank=True, verbose_name='用户电话')
    user_email = models.EmailField(null=True, blank=True, verbose_name='用户邮箱')
    user_class = models.IntegerField(default=0, verbose_name='用户班级')
    user_password = models.CharField(max_length=128, verbose_name='用户密码')
    registration_time = models.DateTimeField(auto_now=True, verbose_name='注册时间')
    last_login_time = models.DateTimeField(auto_now_add=True, verbose_name='上次登录时间')
    user_card = models.CharField(max_length=20, null=True, blank=True, verbose_name='身份证号码')
    user_face = models.JSONField(null=True, blank=True, verbose_name='人脸数据')

    class Meta:
        verbose_name = '用户管理'
        managed = True
        db_table = 'user'


class Grad(models.Model):
    grad_id = models.AutoField(primary_key=True)
    grad_name = models.CharField(max_length=50, verbose_name='行政班级名')
    grad_creator = models.CharField(max_length=50, verbose_name='班级创建者')
    create_time = models.DateTimeField(auto_now=True, verbose_name='班级创建时间')
    grad_introduce = models.TextField(null=True, blank=True, verbose_name='班级介绍')

    class Meta:
        verbose_name = '班级管理'
        managed = True
        db_table = 'grad'


class GradUser(models.Model):
    grad_id = models.IntegerField(verbose_name='班级编号')
    user_id = models.CharField(max_length=50, verbose_name='用户账号')

    class Meta:
        verbose_name = '班级用户'
        managed = True
        db_table = 'grad_user'


class Course(models.Model):
    course_id = models.AutoField(primary_key=True)
    course_name = models.CharField(max_length=50, verbose_name='课程标题')
    course_icon = models.TextField(null=True, blank=True, verbose_name='课程封面(base64)')
    create_time = models.DateTimeField(auto_now=True, verbose_name='创建时间')
    create_user = models.CharField(max_length=50, null=True, blank=True, verbose_name='创建者')

    class Meta:
        verbose_name = '课程'
        managed = True
        db_table = 'course'


class Problem(models.Model):
    PROBLEM_TYPE_CHOICE = [
        (0, '单选题'),
        (1, '多选题'),
        (2, '判断题'),
        (3, '填空题'),
        (4, '解答题')
    ]
    problem_id = models.AutoField(primary_key=True)
    problem_title = models.TextField(verbose_name='问题标题')
    problem_content = models.JSONField(null=True, blank=True, verbose_name='问题内容')
    problem_answer = models.JSONField(null=True, blank=True, verbose_name='问题答案')
    problem_creator = models.CharField(max_length=50, null=True, blank=True, verbose_name='问题创建者')
    problem_type = models.SmallIntegerField(default=0, choices=PROBLEM_TYPE_CHOICE, verbose_name='问题类型')
    create_time = models.DateTimeField(auto_now=True, verbose_name='创建时间')
    problem_course = models.IntegerField(null=True, blank=True, verbose_name='所属课程')

    class Meta:
        verbose_name = '题库'
        managed = True
        db_table = 'problem'


class Task(models.Model):
    TYPE_CHOICE = [
        (0, '测试'),
        (1, '作业'),
        (2, '考试')
    ]

    task_id = models.AutoField(primary_key=True)
    task_title = models.CharField(max_length=50, null=True, blank=True, verbose_name='作业标题')
    task_explain = models.TextField(null=True, blank=True, verbose_name='作业描述')
    task_creator = models.CharField(max_length=50, blank=True, null=True, verbose_name='作业创建者')
    create_time = models.DateTimeField(auto_now=True, verbose_name='创建时间')
    task_type = models.SmallIntegerField(default='0', choices=TYPE_CHOICE, verbose_name='作业类型')
    begin_time = models.DateTimeField(null=True, blank=True, verbose_name='开始时间')
    end_time = models.DateTimeField(null=True, blank=True, verbose_name='结束时间')
    task_status = models.BooleanField(default=True, verbose_name='作业状态')
    task_judge = models.BooleanField(default=True, verbose_name='是否自动判题')
    task_class = models.IntegerField(default='0', verbose_name='所属班级【0为所有班级】')

    class Meta:
        verbose_name = '课程作业管理'
        managed = True
        db_table = 'task'


class TaskQuestion(models.Model):
    task_id = models.IntegerField(verbose_name='作业编号')
    question_id = models.IntegerField(verbose_name='问题编号')
    question_number = models.IntegerField(null=True, blank=True, verbose_name='问题序号')
    question_score = models.IntegerField(null=True, blank=True, verbose_name='问题分数')

    class Meta:
        verbose_name = '作业问题管理'
        managed = True
        db_table = 'take_question'


class TaskSubmit(models.Model):
    submit_id = models.AutoField(primary_key=True)
    task_id = models.IntegerField(verbose_name='作业编号')
    task_user = models.CharField(max_length=50, verbose_name='用户编号')
    create_time = models.DateTimeField(auto_now=True, verbose_name='提交时间')
    use_time = models.IntegerField(null=True, blank=True, verbose_name='花费时间')
    submit_content = models.JSONField(verbose_name='提交答案|json格式存储')
    submit_score = models.IntegerField(default='0', verbose_name='作业分数')
    submit_ip = models.GenericIPAddressField(null=True, blank=True, verbose_name='提交ip地址')
    judge_time = models.DateTimeField(auto_now_add=True, verbose_name='判题时间')
    judge_user = models.CharField(max_length=50, null=True, blank=True, verbose_name='判题者')

    class Meta:
        verbose_name = '作业提交'
        managed = True
        db_table = 'task_submit'
